﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;


namespace ExcelReport
{
    // outputs the damage graph
    class DamageReport
    {
        object misValue;
        Excel.Application excelApp;
        Excel.Workbook workbook;

        public DamageReport()
        {
            misValue = System.Reflection.Missing.Value;
            excelApp = new Excel.Application();
            // Make the object visible.
            excelApp.Visible = true;
            workbook = excelApp.Workbooks.Add(1);
        }

        // for each algo different graph: for each method total damage function over all time slices (total damage=damage for each lun)
        public void algoTotalDamage(string algo)
        {
            Excel.Worksheet worksheet = (Excel.Worksheet)this.excelApp.Worksheets.Add(misValue, misValue, misValue, misValue);
            worksheet = (Excel.Worksheet)workbook.ActiveSheet;
            worksheet.Name = algo+" Total Damage";
            Excel.ChartObjects xlCharts = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
            Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 400, 250);
            Excel.Chart chartPage = myChart.Chart;
            var seriesCol = chartPage.SeriesCollection(misValue);
            chartPage.ChartType = Excel.XlChartType.xlXYScatterLinesNoMarkers;
            int numTimeSlices;
            String line;
            string[] parts;
            string[] names = new string[4];
            int row = 1;
            Boolean first = true;
            char[] delimiters = new char[] { '\t' };
            //reading from file
            worksheet.Cells[row, 1] = "Times";
            row++;
            using (StreamReader sr = new StreamReader("../../../DataBase/OutputFiles/" + algo + "TotalDamageReport.txt"))
            {
                line = sr.ReadLine();
                numTimeSlices = Convert.ToInt32(line);
                while ((line = sr.ReadLine()) != null)
                {
                    parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    worksheet.Cells[row, 1] = parts[0];     // name of method
                    names[row - 2] = parts[0];
                    for (int i = 0; i < numTimeSlices; i++)
                    {
                        line = sr.ReadLine();
                        parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                        worksheet.Cells[row, i+2] = Convert.ToDouble(parts[1]);
                        if (first)
                        {
                            worksheet.Cells[1, i+2] = parts[0];
                        }
                    }
                    first = false;
                    row++;
                }

                Excel.Range chartRange1, chartRange2;
                char cColumn = 'A';
                int series = 1;
                for (int i = 0; i < numTimeSlices; i++)
                    cColumn++;
                String col = cColumn.ToString();
                chartRange2 = worksheet.Range["B1", col + "1"];
                chartRange1 = worksheet.Range["B2", col + "2"];
                seriesCol.add(chartRange1, misValue, misValue, misValue, misValue);
                seriesCol(series).Name = names[0];
                seriesCol(series).XValues = chartRange2;
                series++;
                chartRange1 = worksheet.Range["B3", col + "3"];
                seriesCol.add(chartRange1, misValue, misValue, misValue, misValue);
                seriesCol(series).Name = names[1];
                seriesCol(series).XValues = chartRange2;
                series++;
                chartRange1 = worksheet.Range["B4", col + "4"];
                seriesCol.add(chartRange1, misValue, misValue, misValue, misValue);
                seriesCol(series).Name = names[2];
                seriesCol(series).XValues = chartRange2;
                // add when there is a forth iteration
           /*     series++;
                chartRange1 = worksheet.Range["B5", col + "5"];
                seriesCol.add(chartRange1, misValue, misValue, misValue, misValue);
                seriesCol(series).Name = names[3];
                seriesCol(series).XValues = chartRange2;*/
            }
        }

        public void algoAvgTotalDamage()
        {
        }

        public void methodTotalDamage(string method)
        {
            Excel.Worksheet worksheet = (Excel.Worksheet)this.excelApp.Worksheets.Add(misValue, misValue, misValue, misValue);
            worksheet = (Excel.Worksheet)workbook.ActiveSheet;
            worksheet.Name = method + " Total Damage";
            Excel.ChartObjects xlCharts = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
            Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 400, 250);
            Excel.Chart chartPage = myChart.Chart;
            var seriesCol = chartPage.SeriesCollection(misValue);
            chartPage.ChartType = Excel.XlChartType.xlXYScatterLinesNoMarkers;
            int numTimeSlices, numAlgos=0;
            String line;
            string[] parts;
            string[] names = new string[4];
            int row = 1;
            Boolean first = true;
            char[] delimiters = new char[] { '\t' };
            //reading from file
            worksheet.Cells[row, 1] = "Times";
            row++;
            using (StreamReader sr = new StreamReader("../../../DataBase/OutputFiles/" + method + "TotalDamageReport.txt"))
            {
                line = sr.ReadLine();
                numTimeSlices = Convert.ToInt32(line);
                while ((line = sr.ReadLine()) != null)
                {
                    parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    worksheet.Cells[row, 1] = parts[0];     // name of method
                    names[row - 2] = parts[0];
                    for (int i = 0; i < numTimeSlices; i++)
                    {
                        line = sr.ReadLine();
                        parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                        worksheet.Cells[row, i + 2] = Convert.ToDouble(parts[1]);
                        if (first)
                        {
                            worksheet.Cells[1, i + 2] = parts[0];
                        }
                    }
                    first = false;
                    row++;
                    numAlgos++;
                }

                Excel.Range chartRange1, chartRange2;
                char cColumn = 'A';
                int series = 1;
                for (int i = 0; i < numTimeSlices; i++)
                    cColumn++;
                String col = cColumn.ToString();
                chartRange2 = worksheet.Range["B1", col + "1"];
                for (int i = 0; i < numAlgos; i++, series++)
                {
                    chartRange1 = worksheet.Range["B" + (i + 2), col + (i + 2)];
                    seriesCol.add(chartRange1, misValue, misValue, misValue, misValue);
                    seriesCol(series).Name = names[i];
                    seriesCol(series).XValues = chartRange2;
                }
            }
        }
    }
}
